﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System;

public class SetKeyView : IUIView
{
    public Button up,down,left, right, jump, attack, rush, selectWeapon, back_btn,big,task;
    bool canSetKey = false;
    int Index = -1;
    int oldIndex = -1;
    Tweener tweener;
    Button temp;
    public override void Initinalize()
    {
        up.onClick.AddListener(()=> { Index = 0; });
        down.onClick.AddListener(()=> { Index = 1; });
        left.onClick.AddListener(()=> { Index = 2; });
        right.onClick.AddListener(() => { Index = 3; });
        jump.onClick.AddListener(() => { Index = 4; } );
        attack.onClick.AddListener(() => { Index = 5; } );
        rush.onClick.AddListener(() => { Index = 6; });
        selectWeapon.onClick.AddListener(() => { Index = 7; });
        big.onClick.AddListener(() => { Index = 8; });
        task.onClick.AddListener(() => { Index = 9; });
        back_btn.onClick.AddListener(Return);

    }
    public override void Hide()
    {
        base.Hide();

    }

    private void Update()
    {
        if (oldIndex != Index)
        {
            switch (Index)
            {
                case 0:
                    SetTweener(up);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Up", k);
                        Text t = up.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;
                case 1:
                    SetTweener(down);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Down", k);
                        Text t = down.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;
                case 2:
                    SetTweener(left);
                    InputManager.StartSetKey((k)=> {
                        InputManager.SetKey("Left", k);
                        Text t = left.GetComponentInChildren<Text>();
                        t.text=k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;
                case 3:
                    SetTweener(right);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Right", k);
                        Text t = right.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex=Index = -1;
                    });
                    break;
                case 4:
                    SetTweener(jump);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Jump", k);
                        Text t = jump.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;
                case 5:
                    SetTweener(attack);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Attack", k);
                        Text t = attack.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;
                case 6:
                    SetTweener(rush);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Rush", k);
                        Text t = rush.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;

                case 7:
                    SetTweener(selectWeapon);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("SelectWeapon", k);
                        Text t = selectWeapon.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;
                case 8:
                    SetTweener(big);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Big", k);
                        Text t = big.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;
                case 9:
                    SetTweener(task);
                    InputManager.StartSetKey((k) => {
                        InputManager.SetKey("Task", k);
                        Text t = task.GetComponentInChildren<Text>();
                        t.text = k.ToString();
                        tweener.Pause();
                        t.color = Color.white;
                        oldIndex = Index = -1;
                    });
                    break;

            }
        }


    }

    void InitText()
    {
        switch (Index)
        {
            case 0:
                up.GetComponentInChildren<Text>().color=Color.white;
            break;
            case 1:
                down.GetComponentInChildren<Text>().color=Color.white;
            break;
            case 2:
                left.GetComponentInChildren<Text>().color = Color.white;
                break;
            case 3:
                right.GetComponentInChildren<Text>().color = Color.white;
                break;
            case 4:
                jump.GetComponentInChildren<Text>().color = Color.white;
                break;
            case 5:
                attack.GetComponentInChildren<Text>().color = Color.white;
                break;
            case 6:
                rush.GetComponentInChildren<Text>().color = Color.white;
                break;
            case 7:
                selectWeapon.GetComponentInChildren<Text>().color = Color.white;
                break;
                case 8:
                big.GetComponentInChildren<Text>().color=Color.white;
                break;
                case 9:
                task.GetComponentInChildren<Text>().color=Color.white;
                break;
               
        }
    }
    void SetTweener(Button tr)
    {
        if(temp!=null)
            temp.GetComponentInChildren<Text>().color=Color.white;
        tweener.Pause();
        tweener = tr.GetComponentInChildren<Text>().DOFade(0, 1);
        tweener.SetLoops(-1, LoopType.Yoyo);
        tweener.SetEase(Ease.Linear);
        oldIndex = Index;
        temp = tr;
    }

    public void Return()
    {
        InitText();
        Index = -1;
        tweener.Pause();
        Hide();
        InputManager.activeSetKey = false;
        UIManager.Instance.mainPanel.Show();
    }
    public override void Show()
    {
        Index = oldIndex = -1;
        base.Show();
    }


}
